#include "StdAfx.h"

#define _USE_MATH_DEFINES
#include "DrumSynth.h"
#include <cstdlib>
#include <math.h>

using namespace std;

CDrumSynth::CDrumSynth(void)
{

}


CDrumSynth::~CDrumSynth(void)
{
}

void CDrumSynth::Start( void )
{
	mSineWave.SetSampleRate(GetSampleRate());
	mSineWave.SetAmplitude(1.0);
	mSineWave.Start();

	
	srand( time(NULL) );
	mFreqADSR.SetSource( &mSineWave );
	mFreqADSR.SetSampleRate(GetSampleRate());
	mFreqADSR.SetBeatsPerMinute(GetBPM());
	mFreqADSR.Start();
}

bool CDrumSynth::Generate( void )
{
	
	// Generate a pitch
	mSineWave.Generate();
	// Generate some sort of white noise.
	double lRand = GetRandomNum();

	mFreqADSR.SetWhiteNoise( lRand );
	
	bool lValid = mFreqADSR.Generate();

	mFrame[0] = mFreqADSR.Frame(0);
	mFrame[1] = mFreqADSR.Frame(1);

	return lValid;
}

double CDrumSynth::GetRandomNum( void )
{
	// Generate a random float between 0 and 2
	double lRand = rand();
	double lReturn = 2 * lRand / (double)RAND_MAX;
	// Return that between -1 and 1;
	return lReturn - 1.0;
}